package com.xiaoshuidi.cloud.module.iot.dal.repository;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.mybatis.core.dataobject.BaseDO;
import com.xiaoshuidi.cloud.module.iot.controller.admin.report.vo.WaterElectricityReportReq;
import com.xiaoshuidi.cloud.module.iot.dal.mapper.TbWaterElectricityReportMapper;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbWaterElectricityReport;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

/**
 * @Author Feng
 * @date 2023/9/21
 */
@Repository
public class TbWaterElectricityReportRepository extends ServiceImpl<TbWaterElectricityReportMapper, TbWaterElectricityReport> {

    /**
     * 水电明细 - 分页
     *
     * @param req
     * @return
     */
    public IPage<TbWaterElectricityReport> page(WaterElectricityReportReq req) {
        Page<TbWaterElectricityReport> page = Page.of(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<TbWaterElectricityReport> wrapper = this.pageWrapper(req);
        return page(page, wrapper);
    }

    public Long exportCount(WaterElectricityReportReq req) {
        LambdaQueryWrapper<TbWaterElectricityReport> wrapper = this.pageWrapper(req);
        return this.count(wrapper);

    }

    private LambdaQueryWrapper<TbWaterElectricityReport> pageWrapper(WaterElectricityReportReq req) {
        LambdaQueryWrapper<TbWaterElectricityReport> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(req.getApartmentId() != null, TbWaterElectricityReport::getApartmentId, req.getApartmentId())
                .eq(StrUtil.isNotEmpty(req.getUndertakingParty()), TbWaterElectricityReport::getUndertakingParty, req.getUndertakingParty())
                .ge(StrUtil.isNotEmpty(req.getStartTime()), BaseDO::getCreateTime, req.getStartTime())
                .eq(StrUtil.isNotEmpty(req.getFeeType()), TbWaterElectricityReport::getFeeType, req.getFeeType())
                .like(StrUtil.isNotEmpty(req.getRoomName()), TbWaterElectricityReport::getRoomName, req.getRoomName())
                .like(StrUtil.isNotEmpty(req.getPhone()), TbWaterElectricityReport::getPhone, req.getPhone())
                .like(StrUtil.isNotEmpty(req.getMemberName()), TbWaterElectricityReport::getMemberName, req.getMemberName())
                .orderByDesc(TbWaterElectricityReport::getId);
        if (StrUtil.isNotEmpty(req.getEndTime())) {
            LocalDateTime endTime = LocalDateTimeUtil.parse(req.getEndTime(), "yyyy-MM-dd");
            endTime = endTime.plusDays(1);
            String endTimeStr = LocalDateTimeUtil.format(endTime, "yyyy-MM-dd");
            wrapper.le(BaseDO::getCreateTime, endTimeStr);
        }
        return wrapper;
    }
}
